#include "StdAfx.h"
#include "EditDialog.h"

#include "Utils.h"

LRESULT EditDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    AtlCreateMenuBar(m_hWnd, IDR_EDIT_MENU, SHCMBF_HMENU);   

    memoCtrl.SubclassWindow(GetDlgItem(IDC_RICHINK));
    memoCtrl.SetFocus();

    if (utils::FileExists(fileName))
        memoCtrl.Load(fileName, unicode);

    return bHandled = FALSE;
}

LRESULT EditDialog::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    if (utils::FileExists(fileName) || memoCtrl.GetTextLength())
        memoCtrl.Save(fileName, unicode);

    return bHandled = FALSE;
}

void EditDialog::OnSize(UINT, const CSize &size)
{
    CRect rect;
    GetClientRect(&rect);
    memoCtrl.SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(), SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
}

LRESULT EditDialog::OnContextMenu(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
    PNMRGINFO pnmrgi = (PNMRGINFO)pnmh ;
    CMenuHandle menu;
    
    menu.LoadMenu(IDR_EDIT_MENU);
    menu = menu.GetSubMenu(0);

    return menu.TrackPopupMenu( TPM_HORIZONTAL | TPM_VERTICAL, pnmrgi->ptAction.x, pnmrgi->ptAction.y, m_hWnd);
}
